import { getMemuList, MenuResponse } from "@/services/api/user";
import { message } from "antd";
import { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { setMenu } from "@/store/user/authSlice";
import { StateType } from "@/store";

export default function useLoadMenuData() {
  const dispatch = useDispatch();
  const {token} = useSelector((state: StateType) => state.authSlice);
  const [menuList, setMenuList] = useState<MenuResponse[]>([]);

  useEffect(() => {
    loadMenuData();
  }, [token]);

  async function loadMenuData() {
    try {
      const items = await getMemuList();
      dispatch(setMenu(items));
      setMenuList(items);
    } catch (err) {
      message.error("菜单列表获取失败！");
      console.log(err);
    }
  }

  return { menuList };
}
